package com.centit.framework.ip.businessdata.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.ip.businessdata.po.InterfaceManager;
import com.centit.framework.jdbc.dao.BaseDaoImpl;
import com.centit.framework.jdbc.dao.DatabaseOptUtils;
import com.centit.support.database.utils.PageDesc;
import com.centit.support.database.utils.QueryAndNamedParams;
import com.centit.support.database.utils.QueryUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;


/**
 * InterfaceManagerDao  Repository.
 * create by scaffold 2018-05-22
 *
 * @author guo_jh
 * T_INTERFACE_MANAGER业务系统接口配置信息表
 */

@Repository
public class InterfaceManagerDao extends BaseDaoImpl<InterfaceManager, String> {

    public static final Log log = LogFactory.getLog(InterfaceManagerDao.class);

    @Override
    public Map<String, String> getFilterField() {
        if (filterField == null) {
            filterField = new HashMap<String, String>();

            filterField.put("recordid", CodeBook.EQUAL_HQL_ID);


            filterField.put("systemflag", CodeBook.EQUAL_HQL_ID);

            filterField.put("systemurl", CodeBook.EQUAL_HQL_ID);

            filterField.put("systemname", CodeBook.EQUAL_HQL_ID);

            filterField.put("startOrStop", CodeBook.EQUAL_HQL_ID);

            filterField.put("creater", CodeBook.EQUAL_HQL_ID);

            filterField.put("createTime", CodeBook.EQUAL_HQL_ID);

            filterField.put("updater", CodeBook.EQUAL_HQL_ID);

            filterField.put("updateTime", CodeBook.EQUAL_HQL_ID);

        }
        return filterField;
    }


    /**
     * 获取子系统信息集合
     *
     * @param filterMap
     * @param pageDesc
     * @return
     */
    public JSONArray listObjectsAsJson(Map<String, Object> filterMap, PageDesc pageDesc) {
        String sql = "SELECT T1.RECORDID AS ID, T1.RECORDID, T1.SYSTEMNAME AS NAME, '0' AS parentid, 'system' AS DATATYPE,to_char(t1.create_time,'yyyy-MM-dd hh24:mi:ss') as create_time, " +
                " T1.SYSTEMURL AS URL,NVL2(T2.RECORDID, 'closed', NULL) AS STATE FROM T_INTERFACE_MANAGER T1 LEFT JOIN (SELECT T.RECORDID FROM T_INTERFACE_METHOD T " +
                " WHERE 1 = 1 [ :startorstop | AND T.START_OR_STOP = :startorstop ]  GROUP BY T.RECORDID) T2 ON T1.RECORDID = T2.RECORDID  WHERE 1 = 1 " +
                " [ :(like)systemname | and t1.systemname like :systemname ] " +
//                " [ :startorstop | and T1.START_OR_STOP = :startorstop ] " +
                " and T1.START_OR_STOP = 'T' " +
                " order by t1.create_time desc ";
        QueryAndNamedParams params = QueryUtils.translateQuery(sql, filterMap);
        JSONArray listObjects = DatabaseOptUtils.listObjectsByNamedSqlAsJson(this, params.getQuery(), params.getParams(), null);
        return listObjects;
    }

    /**
     * 获取接口信息
     *
     * @param filterMap 筛选条件
     * @return
     */
    public JSONObject getObjectAsJson(Map<String, Object> filterMap) {
        String sql = " SELECT T2.SYSTEMFLAG, T1.METHODFLAG, T1.METHODURL, T1.SQLCONTENT FROM T_INTERFACE_METHOD T1 " +
                " INNER JOIN T_INTERFACE_MANAGER T2 ON T1.RECORDID = T2.RECORDID WHERE T1.START_OR_STOP = 'T' AND T2.START_OR_STOP = 'T' " +
                " [ :methodflag | AND T1.METHODFLAG = :methodflag ] " +
                " [ :systemflag | AND T2.SYSTEMFLAG = :systemflag ] " +
                " [ :(like)systemurl | AND T2.SYSTEMURL LIKE :systemurl ] ";
        QueryAndNamedParams params = QueryUtils.translateQuery(sql, filterMap);
        JSONObject object = DatabaseOptUtils.getObjectBySqlAsJson(this, params.getQuery(), params.getParams());
        return object;
    }

    public JSONArray listBySqlAsJson(String sql, Map<String, Object> filterMap) {
        QueryAndNamedParams params = QueryUtils.translateQuery(sql, filterMap);
        JSONArray listObjects = DatabaseOptUtils.listObjectsByNamedSqlAsJson(this, params.getQuery(), params.getParams(), null);
        return listObjects;
    }

    /**
     * 业务系统信息筛选
     *
     * @param filterMap
     * @param pageDesc
     * @return
     */
    public JSONArray listObjectsAsJsonArray(Map<String, Object> filterMap, PageDesc pageDesc) {
        String sql = " SELECT T.RECORDID,T.SYSTEMNAME,T.SYSTEMURL,TO_CHAR(T.CREATE_TIME, 'yyyy-MM-dd HH24:mi:ss') AS CREATE_TIME,T.START_OR_STOP FROM T_INTERFACE_MANAGER T WHERE 1 = 1 " +
                " [ :(like)systemname|AND T.SYSTEMNAME LIKE :systemname ] " +
                " [ :startorstop|AND T.START_OR_STOP = :startorstop ] " +
                " ORDER BY T.CREATE_TIME DESC ";
        QueryAndNamedParams params = QueryUtils.translateQuery(sql, filterMap);
        JSONArray jsonArray = DatabaseOptUtils.listObjectsByNamedSqlAsJson(this, params.getQuery(), params.getParams(), pageDesc);
        return jsonArray;
    }
}
